package com.jspgou.cms.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.springframework.stereotype.Repository;

import com.jspgou.common.hibernate3.Finder;
import com.jspgou.common.hibernate3.HibernateBaseDao;
import com.jspgou.common.page.Pagination;
import com.jspgou.cms.dao.ShopYushouDao;
import com.jspgou.cms.entity.Order;
import com.jspgou.cms.entity.ShopYushou;
import com.jspgou.cms.entity.ShopMember;

@Repository
public class ShopYushouDaoImpl extends HibernateBaseDao<ShopYushou, Long> implements ShopYushouDao {
	public Pagination getPage(int pageNo, int pageSize) {
		Criteria crit = createCriteria();
		Pagination page = findByCriteria(crit, pageNo, pageSize);
		return page;
	}

	public ShopYushou findById(Long id) {
		ShopYushou entity = get(id);
		return entity;
	}
	
	@SuppressWarnings("unchecked")
	public ShopYushou findByCode(String code){
		Finder f=Finder.create("from ShopYushou bean where bean.code=:code").setParam("code", code);
		List<ShopYushou> list=find(f);
		if(list.size()==1){
			return list.get(0);
		}else{
			return null;
		}
	}

	public ShopYushou save(ShopYushou bean) {
		getSession().save(bean);
		return bean;
	}

	public ShopYushou deleteById(Long id) {
		ShopYushou entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}
	
	@Override
	protected Class<ShopYushou> getEntityClass() {
		return ShopYushou.class;
	}

	public List<ShopYushou> getList(ShopMember member, short status) {
		Finder f=Finder.create("from ShopYushou bean where 1=1");
		if(member!=null){
			f.append(" and bean.user =:user");
			f.setParam("user", member);
		}
		if (status!=8) {
			f.append(" and bean.ystatus =:ystatus");
			f.setParam("ystatus", status);
		}
		f.append(" order by bean.createdate desc");
		return find(f);
	}
	
	/**
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @param member
	 * @param storeId 店铺id
	 * @param createTime 预售订单创建时间
	 * @param status 预售订单状态
	 * @return
	 */
	public Pagination getPage(int pageNo, int pageSize, Long memberId,Long storeId,Date createTime,
			Short status) {
		Finder f=Finder.create("from ShopYushou bean where 1=1");
		if(memberId!=null){
			f.append(" and bean.user.id =:userId");
			f.setParam("userId", memberId);
		}
		if(storeId!=null){
			f.append(" and bean.productid.store.id =:storeId");
			f.setParam("storeId", storeId);
		}
		if(createTime!=null){
			f.append(" and bean.createdate =:createTime");
			f.setParam("createTime", createTime);
		}
		if (status!=null) {
			f.append(" and bean.ystatus =:ystatus");
			f.setParam("ystatus", status);
		}
		f.append(" order by bean.createdate desc");
		return find(f,pageNo, pageSize);
	}
}